package net.edu_soft.ec.asset.controller;


import com.github.pagehelper.PageInfo;
import net.edu_soft.ec.asset.pojo.Permission;
import net.edu_soft.ec.asset.pojo.Role;
import net.edu_soft.ec.asset.service.PermissionService;
import net.edu_soft.ec.asset.service.RolePermissionService;
import net.edu_soft.ec.common.util.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  @author: PUING
 *  @Date: 2020/6/10 20:35
 *  @Description: 权限控制类
 */
@RestController
@RequestMapping(value = "/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RolePermissionService rolePermissionService;

    /***
     * 查询权限信息
     * @param permissionId 权限id
     * @return 权限信息
     */
    @RequestMapping(value = "/find")
    @RequiresPermissions("permission:find")
    public ResultVo find(Integer permissionId) {
        Permission permission = permissionService.find(permissionId);
        if (permission == null) {
            return ResultVo.failed(400, "查询失败，权限信息为空", permission);
        }
        return ResultVo.succeed("查询成功", permission);
    }


    /***
     * 分页查询权限列表
     * @param pageNum 当前页
     * @param pageSize 每页数
     * @return 分页信息
     */
    @RequestMapping(value = "/findList")
    @RequiresPermissions("permission:findList")
    public ResultVo findList(String name,Integer pageNum, Integer pageSize) {
        PageInfo<Permission> list = permissionService.findList(name,pageNum, pageSize);
        return ResultVo.succeed("查询结果", list);
    }


    /***
     * 添加权限
     * @param permission 权限信息
     * @param code 菜单权限码 1为菜单 2位权限
     * @return
     */
    @RequestMapping(value = "/save")
    @RequiresPermissions("permission:save")
    public ResultVo save(Permission permission, Integer code) {
        String m = permissionService.save(permission, code) == 1 ? "添加成功" : "添加失败";
        return ResultVo.succeed(m, null);
    }


    /***
     * 修改权限
     * @param permission 权限信息
     * @param code 菜单权限码 1为菜单 2位权限
     * @return
     */
    @RequestMapping(value = "/update")
    @RequiresPermissions("permission:update")
    public ResultVo update(Permission permission, Integer code) {
        String m = permissionService.update(permission,code) == 1 ? "修改成功" : "修改失败";
        return ResultVo.succeed(m, null);
    }


    /***
     * 删除权限
     * @param ids id串
     * @param isDel 是否删除 1删除0不删除
     * @return
     */
    @RequestMapping(value = "/del")
    @RequiresPermissions("permission:del")
    public ResultVo del(String ids,Integer isDel) {
        String m = permissionService.del(ids,isDel) >= 1 ? "删除成功" : "删除失败";
        return ResultVo.succeed(m, null);
    }


    /***
     * 为角色添加权限
     * @param roleId 角色id
     * @param permissionIds 权限id串
     * @return
     */
    @RequestMapping(value = "/saveRolePermission")
    @RequiresPermissions("permission:saveRolePermission")
    public ResultVo saveRolePermission(Integer roleId,String permissionIds) {
        String m = rolePermissionService.save(roleId,permissionIds) >= 1 ? "添加成功" : "添加失败";
        return ResultVo.succeed(m, null);
    }


    /***
     * 为角色删除权限
     * @param roleId 角色id
     * @param permissionIds 权限id串
     * @param isDel 1删除 0不删除
     * @return
     */
    @RequestMapping(value = "/delRolePermission")
    @RequiresPermissions("permission:delRolePermission")
    public ResultVo delRolePermission(Integer roleId,String permissionIds,Integer isDel) {
        String m = rolePermissionService.del(roleId,permissionIds,isDel) >= 1 ? "删除成功" : "删除失败";
        return ResultVo.succeed(m, null);
    }



    /***
     * 查询某个角色的权限菜单
     * @param roleId 角色id
     * @return
     */
    @RequestMapping(value = "/findByRoleId")
    @RequiresPermissions("permission:findByRoleId")
    public ResultVo findByRoleId(Integer roleId){
        List<Permission> permissions = permissionService.findByRoleId(roleId);
        if (permissions==null || permissions.size()<=0) {
            return ResultVo.failed(400, "查询失败，权限信息为空,roleId:", roleId);
        }
        return ResultVo.succeed("查询成功", permissions);
    }

    /***
     * 根据父id查询权限菜单
     * @param parentId 父id
     * @return
     */
    @RequestMapping(value = "/findByParentId")
    @RequiresPermissions("permission:findByParentId")
    public ResultVo findByParentId(Integer parentId){
        List<Permission> permissions = permissionService.findByParentId(parentId);
        if (permissions==null || permissions.size()<=0) {
            return ResultVo.failed(400, "查询失败，权限信息为空,parentId:", parentId);
        }
        return ResultVo.succeed("查询成功", permissions);
    }

}
